import {
  VuexModule,
  Module,
  Mutation,
  getModule
} from "vuex-module-decorators";
import store from "@/store";
import { menuConfig } from "@/config/menuConfig";
import { isLength, isEqual, isUndefined, isEmpty } from "lodash";

@Module({ dynamic: true, name: "aside", store })
export default class Aside extends VuexModule {
  private _key = "0";

  get activeKey() {
    return this._key;
  }

  @Mutation
  SET_ACTIVE_KEY(name: string | null | undefined) {
    if (isEmpty(name) || isUndefined(name)) return;

    let key = "";
    menuConfig.forEach((element, index) => {
      if (isEqual(element.name, name)) {
        key = index + "";
      } else if (
        !isUndefined(element.children) &&
        isLength(element.children.length)
      ) {
        element.children.forEach((eChildren, iChildren) => {
          if (isEqual(eChildren.name, name)) {
            key = `${index}-${iChildren}`;
          }
        });
      }
    });

    this._key = key;
  }
}

export const AsideModule = getModule(Aside);
